﻿//-----------------------------------------------------------------------
// <summary>
//  A module in Prism is a logical unit in your application.
// </summary>
// <copyright file="NewsFeedService.cs" company="DSSoftLogic">
//     Copyright@ DSSoftLogic 2011, Non-Commercial. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace Property.Silverlight.Model.Services
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.Composition;
    using System.Globalization;
    using System.Linq;
    using System.Xml.Linq;
    using Property.Silverlight.Model.Resources;

    /// <summary>
    /// This NewsArticle is testing the StyleCop rules and how to use them.
    /// </summary>
    [Export(typeof(INewsFeedService))]
    [PartCreationPolicy(CreationPolicy.Shared)]
    public class NewsFeedService : INewsFeedService
    {
        /// <summary>
        /// The news data.
        /// </summary>
        private readonly Dictionary<string, List<NewsArticle>> newsData = new Dictionary<string, List<NewsArticle>>();

        /// <summary>
        /// Initializes a new instance of the <see cref="NewsFeedService"/> class.
        /// </summary>
        public NewsFeedService()
        {
            var document = XDocument.Parse(Property.Silverlight.Model.Resources.Resource.News);
            this.newsData = document.Descendants("NewsItem").GroupBy(
                x => x.Attribute("TickerSymbol").Value,
                x => new NewsArticle
                    {
                        PublishedDate = DateTime.Parse(x.Attribute("PublishedDate").Value, CultureInfo.InvariantCulture),
                        Title = x.Element("Title").Value,
                        Body = x.Element("Body").Value,
                        IconUri = x.Attribute("IconUri") != null ? x.Attribute("IconUri").Value : null
                    })
                .ToDictionary(group => group.Key, group => group.ToList());
        }

        #region INewsFeed Members

        /// <summary>
        /// The updated.
        /// </summary>
        public event EventHandler<NewsFeedEventArgs> Updated = delegate { };

        /// <summary>
        /// The get news.
        /// </summary>
        /// <param name="tickerSymbol">
        /// The ticker symbol.
        /// </param>
        /// <returns>
        /// The <see cref="IList"/>.
        /// </returns>
        public IList<NewsArticle> GetNews(string tickerSymbol)
        {
            List<NewsArticle> articles = new List<NewsArticle>();
            this.newsData.TryGetValue(tickerSymbol, out articles);
            return articles;
        }

        /// <summary>
        /// The has news.
        /// </summary>
        /// <param name="tickerSymbol">
        /// The ticker symbol.
        /// </param>
        /// <returns>
        /// The <see cref="bool"/>.
        /// </returns>
        public bool HasNews(string tickerSymbol)
        {
            return this.newsData.ContainsKey(tickerSymbol);
        }

        #endregion
    }
}
